#include<bits/stdc++.h>
using namespace std;

class Solution {
public:

    vector<vector<int>> threeSum(vector<int>& nums) {

        sort(nums.begin(),nums.end());
        vector<vector<int>> res;
        if (nums.size()<3) return res;
        int len = nums.size();
        
        for(int i=0;i<len;i++){
            if (i>0&&nums[i]==nums[i-1]) continue; //刨除重复元组
           int target = -nums[i];
         
           for(int L = i+1,R = len-1;L<R;){
       
               if (L>i+1&&nums[L-1]==nums[L]){//刨除重复元组
                    L++;
                    continue;
               }
               int cur = nums[L] + nums[R];
               if (cur==target){
                   res.push_back({nums[i],nums[L],nums[R]});
                   L++;//这里一定不能少，否则就无法跳出循环了
               }else if (cur>target)
                   R--;
               else L++;
           }
        }
        return res;

    }
};
int main()
{
	Solution s;
	vector<int> in = {-1,0,1,2,-1,-4};
	vector<vector<int>> res  =  s.threeSum(in) ;
	for(int i=0;i<res.size();i++)
	{
		for(int j=0;j<3;j++){
			cout<<res[i][j]<<" ";
		}cout<<endl;
	}
	return 0;
}